package com.wl.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wl.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getById/{id}")
    @HystrixCommand(fallbackMethod = "orderFallbackMethod")
    public User getById(@PathVariable("id") Long id) {
        //用户的url
        String url = "http://user-service/user/getById/" + id;
        //发送请求
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    /**
     * 托底方法
     */
    public User orderFallbackMethod(@PathVariable("id")Long id){
        return new User(-1L,"触发托底","系统繁忙！！请稍后重试");
    }
}